package com.bhy702.generator.builder.beetl;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;

/**
 * @author: brbai
 * @create: 2019-10-16 17:12:45
 * @description:
 */
@Configuration
@Slf4j
@Data
public class BeetlConfiguration {

    @Value("${beetl.template-path}")
    private String templatePath;

    @Value("${beetl.delimiter-statement-start}")
    private String delimiterStatementStart;

    @Value("${beetl.delimiter-statement-end}")
    private String delimiterStatementEnd;

    @Bean(name = "beetlConfig")
    public BeetlGroupUtilConfiguration beetlGroupUtilConfiguration() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();

        // Beetl的配置
        Properties properties = new Properties();
        properties.setProperty("statementStart",delimiterStatementStart);
        properties.setProperty("statementEnd",delimiterStatementEnd);
        properties.setProperty("DELIMITER_STATEMENT_START", delimiterStatementStart);
        properties.setProperty("DELIMITER_STATEMENT_END", delimiterStatementEnd);
        beetlGroupUtilConfiguration.setConfigProperties(properties);

        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(templatePath);

        beetlGroupUtilConfiguration.setResourceLoader(resourceLoader);
        // 调用Beetl的初始化方法
        beetlGroupUtilConfiguration.init();

        return beetlGroupUtilConfiguration;
    }

}